Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / service / src / commonMain / kotlin / org / meshtastic / core / service / MeshServiceOrchestrator.kt

Displaying Raw • Download

core/service/src/commonMain/kotlin/org/meshtastic/core/service/MeshServiceOrchestrator.kt 174315b21f5affcb952642f1650d316c86b8a40f (174315b2) Text, 6.42 KB

T8b949e/*
* Copyright (c) 2025-2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.service

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787co.touchlab.kermit.Severity
Tff7b72import T7ee787kotlinx.coroutines.CoroutineScope
Tff7b72import T7ee787kotlinx.coroutines.Job
Tff7b72import T7ee787kotlinx.coroutines.flow.launchIn
Tff7b72import T7ee787kotlinx.coroutines.flow.onEach
Tff7b72import T7ee787org.koin.core.annotation.Named
Tff7b72import T7ee787org.koin.core.annotation.Single
Tff7b72import T7ee787org.meshtastic.core.common.database.DatabaseManager
Tff7b72import T7ee787org.meshtastic.core.common.util.handledLaunch
Tff7b72import T7ee787org.meshtastic.core.repository.MeshMessageProcessor
Tff7b72import T7ee787org.meshtastic.core.repository.MeshRouter
Tff7b72import T7ee787org.meshtastic.core.repository.MeshServiceNotifications
Tff7b72import T7ee787org.meshtastic.core.repository.NodeManager
Tff7b72import T7ee787org.meshtastic.core.repository.RadioInterfaceService
Tff7b72import T7ee787org.meshtastic.core.repository.ServiceRepository
Tff7b72import T7ee787org.meshtastic.core.repository.TakPrefs
Tff7b72import T7ee787org.meshtastic.core.takserver.TAKMeshIntegration
Tff7b72import T7ee787org.meshtastic.core.takserver.TAKServerManager

T8b949e/**
* Platform-agnostic orchestrator for the mesh service lifecycle.
*
* Extracts the startup wiring previously embedded in Android's `MeshService.onCreate()` into a reusable component. Both
* Android's foreground `Service` and the Desktop `main()` function can use this to start/stop the mesh service graph.
*
* All injected dependencies are `commonMain` interfaces with real implementations in `core:data`.
*/
Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffLongParameterListTa5d6ff"Tb4b4b4)
Tf0883e@Single
Tff7b72class T56d364MeshServiceOrchestratorTb4b4b4(
Tff7b72private Tff7b72val Te6edf3radioInterfaceServiceTb4b4b4: Te6edf3RadioInterfaceServiceTb4b4b4,
Tff7b72private Tff7b72val Te6edf3serviceRepositoryTb4b4b4: Te6edf3ServiceRepositoryTb4b4b4,
Tff7b72private Tff7b72val Te6edf3nodeManagerTb4b4b4: Te6edf3NodeManagerTb4b4b4,
Tff7b72private Tff7b72val Te6edf3messageProcessorTb4b4b4: Te6edf3MeshMessageProcessorTb4b4b4,
Tff7b72private Tff7b72val Te6edf3routerTb4b4b4: Te6edf3MeshRouterTb4b4b4,
Tff7b72private Tff7b72val Te6edf3serviceNotificationsTb4b4b4: Te6edf3MeshServiceNotificationsTb4b4b4,
Tff7b72private Tff7b72val Te6edf3takServerManagerTb4b4b4: Te6edf3TAKServerManagerTb4b4b4,
Tff7b72private Tff7b72val Te6edf3takMeshIntegrationTb4b4b4: Te6edf3TAKMeshIntegrationTb4b4b4,
Tff7b72private Tff7b72val Te6edf3takPrefsTb4b4b4: Te6edf3TakPrefsTb4b4b4,
Tff7b72private Tff7b72val Te6edf3databaseManagerTb4b4b4: Te6edf3DatabaseManagerTb4b4b4,
Tf0883e@NamedTb4b4b4(Ta5d6ff"Ta5d6ffServiceScopeTa5d6ff"Tb4b4b4) Tff7b72private Tff7b72val Te6edf3scopeTb4b4b4: Te6edf3CoroutineScopeTb4b4b4,
Tb4b4b4) Tb4b4b4{
Tff7b72private Tff7b72var Te6edf3serviceJobTb4b4b4: Te6edf3Job? Tff7b72= Tff7b72null
Tff7b72private Tff7b72var Te6edf3takJobTb4b4b4: Te6edf3Job? Tff7b72= Tff7b72null

T8b949e/** The coroutine scope for the service. */
Tff7b72val Te6edf3serviceScopeTb4b4b4: Te6edf3CoroutineScope
Tff7b72getTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3scope

T8b949e/** Whether the orchestrator is currently running. */
Tff7b72val Te6edf3isRunningTb4b4b4: Tffa657Boolean
Tff7b72getTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3serviceJobTff7b72?.Te6edf3isActive Tff7b72=Tff7b72= Tff7b72true

T8b949e/**
* Starts the mesh service components and wires up data flows.
*
* This is the KMP equivalent of `MeshService.onCreate()`. It connects to the radio and wires incoming radio data to
* the message processor and service actions to the router's action handler.
*/
Tff7b72fun Td2a8ffstartTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3isRunningTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3d Tb4b4b4{ Ta5d6ff"Ta5d6ffstart() called while already running, ignoringTa5d6ff" Tb4b4b4}
Tff7b72return
Tb4b4b4}

Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffStarting mesh service orchestratorTa5d6ff" Tb4b4b4}
Tff7b72val Te6edf3job Tff7b72= Te6edf3JobTb4b4b4(Tb4b4b4)
Te6edf3serviceJob Tff7b72= Te6edf3job

Te6edf3serviceNotificationsTb4b4b4.Te6edf3initChannelsTb4b4b4(Tb4b4b4)

T8b949e// Observe TAK server pref to start/stop
Te6edf3takJob Tff7b72=
Te6edf3takPrefsTb4b4b4.Te6edf3isTakServerEnabled
Tb4b4b4.Te6edf3onEach Tb4b4b4{ Te6edf3isEnabled Tff7b72-Tff7b72>
Tff7b72if Tb4b4b4(Te6edf3isEnabled Tff7b72&Tff7b72& Tff7b72!Te6edf3takServerManagerTb4b4b4.Te6edf3isRunningTb4b4b4.Te6edf3valueTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffTAK Server enabled by preference, starting integrationTa5d6ff" Tb4b4b4}
Te6edf3takMeshIntegrationTb4b4b4.Te6edf3startTb4b4b4(Te6edf3scopeTb4b4b4)
Tb4b4b4} Tff7b72else Tff7b72if Tb4b4b4(Tff7b72!Te6edf3isEnabled Tff7b72&Tff7b72& Te6edf3takServerManagerTb4b4b4.Te6edf3isRunningTb4b4b4.Te6edf3valueTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffTAK Server disabled by preference, stopping integrationTa5d6ff" Tb4b4b4}
Te6edf3takMeshIntegrationTb4b4b4.Te6edf3stopTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4.Te6edf3launchInTb4b4b4(Te6edf3scopeTb4b4b4)

Te6edf3scopeTb4b4b4.Te6edf3handledLaunch Tb4b4b4{
T8b949e// Ensure the per-device database is active before the radio connects.
T8b949e// On Android this is handled by MeshUtilApplication.init(); on Desktop (and any
T8b949e// future KMP host) the orchestrator is the first entry point, so it must initialize
T8b949e// the database here. Without this, DatabaseManager._currentDb stays null and all
T8b949e// Room writes via withDb() are silently dropped — causing ourNodeInfo to remain null
T8b949e// after the handshake completes.
Te6edf3databaseManagerTb4b4b4.Te6edf3switchActiveDatabaseTb4b4b4(Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3getDeviceAddressTb4b4b4(Tb4b4b4)Tb4b4b4)
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffPer-device database initialized, connecting radioTa5d6ff" Tb4b4b4}
Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3connectTb4b4b4(Tb4b4b4)
Tb4b4b4}

Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3receivedData
Tb4b4b4.Te6edf3onEach Tb4b4b4{ Te6edf3bytes Tff7b72-Tff7b72> Te6edf3messageProcessorTb4b4b4.Te6edf3handleFromRadioTb4b4b4(Te6edf3bytesTb4b4b4, Te6edf3nodeManagerTb4b4b4.Te6edf3myNodeNumTb4b4b4.Te6edf3valueTb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3launchInTb4b4b4(Te6edf3scopeTb4b4b4)

Te6edf3radioInterfaceServiceTb4b4b4.Te6edf3connectionError
Tb4b4b4.Te6edf3onEach Tb4b4b4{ Te6edf3errorMessage Tff7b72-Tff7b72> Te6edf3serviceRepositoryTb4b4b4.Te6edf3setErrorMessageTb4b4b4(Te6edf3errorMessageTb4b4b4, Te6edf3SeverityTb4b4b4.Te6edf3WarnTb4b4b4) Tb4b4b4}
Tb4b4b4.Te6edf3launchInTb4b4b4(Te6edf3scopeTb4b4b4)

T8b949e// Each action is dispatched in its own supervised coroutine so that a failure in one
T8b949e// action (e.g. a timeout in sendAdminAwait) cannot terminate the collector and silently
T8b949e// drop all subsequent service actions for the rest of the session.
Te6edf3serviceRepositoryTb4b4b4.Te6edf3serviceAction
Tb4b4b4.Te6edf3onEach Tb4b4b4{ Te6edf3action Tff7b72-Tff7b72> Te6edf3scopeTb4b4b4.Te6edf3handledLaunch Tb4b4b4{ Te6edf3routerTb4b4b4.Te6edf3actionHandlerTb4b4b4.Te6edf3onServiceActionTb4b4b4(Te6edf3actionTb4b4b4) Tb4b4b4} Tb4b4b4}
Tb4b4b4.Te6edf3launchInTb4b4b4(Te6edf3scopeTb4b4b4)

Te6edf3nodeManagerTb4b4b4.Te6edf3loadCachedNodeDBTb4b4b4(Tb4b4b4)
Tb4b4b4}

T8b949e/**
* Stops the mesh service components and cancels the coroutine scope.
*
* This is the KMP equivalent of `MeshService.onDestroy()`.
*/
Tff7b72fun Td2a8ffstopTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Ta5d6ffStopping mesh service orchestratorTa5d6ff" Tb4b4b4}
Te6edf3takJobTff7b72?.Te6edf3cancelTb4b4b4(Tb4b4b4)
Te6edf3takJob Tff7b72= Tff7b72null
T8b949e// Guard stop() so we don't emit a spurious "stopped" log when TAK was never started
Tff7b72if Tb4b4b4(Te6edf3takServerManagerTb4b4b4.Te6edf3isRunningTb4b4b4.Te6edf3valueTb4b4b4) Tb4b4b4{
Te6edf3takMeshIntegrationTb4b4b4.Te6edf3stopTb4b4b4(Tb4b4b4)
Tb4b4b4}
Te6edf3serviceJobTff7b72?.Te6edf3cancelTb4b4b4(Tb4b4b4)
Te6edf3serviceJob Tff7b72= Tff7b72null
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.2 - Generated in 0.04s